﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.Drawing;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS_钉钉签收消息提醒
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMStoDDmsgSendController : ControllerBase
    {
        private readonly ILogger<TMStoDDmsgSendRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string srm_con;


        public TMStoDDmsgSendController(ILogger<TMStoDDmsgSendRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            srm_con = _configuration.GetConnectionString("SRM_DbConnection");

        }

        /// <summary>
        /// TMS_钉钉签收消息提醒
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostDDmsgSend([FromBody] TMStoDDmsgSendRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMStoDDmsgSendResponseDto re = new TMStoDDmsgSendResponseDto();
            TMStoDDmsgSend_RqDto rq = new TMStoDDmsgSend_RqDto();
            string source_no = strparm.source_no;
            string price_no = "SRM"+"-"+DateTime.Now.ToString("yyMMdd");
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic


            try
            {
                //同步TMS现有运价状态
                dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("source_no", source_no);
                DataTable tms_yf_dt = MethodHelp.GetTableInfo("TMS0121", dic, con);//获取需要提醒上传附件数据源
                if (tms_yf_dt.Rows.Count > 0)
                {
                    TMStoDDmsgSend_AtRqDto at = new TMStoDDmsgSend_AtRqDto();
                    TMStoDDmsgSend_ContentRqDto text = new TMStoDDmsgSend_ContentRqDto();                   
                    string content = "";
                    dic = new Dictionary<string, string>(); //查询公用dic
                    dic.Add("source_no", source_no);
                    DataTable tms_cy_dt = MethodHelp.GetTableInfo("TMS0122", dic, con);//涉及承运商联系电话获取
                    if (tms_cy_dt.Rows.Count > 0)
                    {
                        List<string> atm = new List<string>();
                        foreach (DataRow row in tms_yf_dt.Rows)
                        {
                            if (row["contacttel"].ToString().Trim() != "")
                            {
                                atm.Add(row["contacttel"].ToString());
                            }

                        }
                        if (atm != null)
                        {
                            at.atMobiles = atm;
                            at.isAtAll = false;
                        }
                    }

                    foreach (DataRow row in tms_yf_dt.Rows)
                    {
                        content = content + "\r\n基地:"+ row["factory_code"].ToString() + "|运单:" + row["waybill_no"].ToString() + "|车牌号:" + row["car_no"].ToString() + "|承运商:" + row["carrier_shortname"].ToString()+ "|计划签收时间:" 
                            + DB.ToDateTime(row["rearrived_date"].ToString()).ToString("yyyy/MM/dd") + "|逾期天数:"+ row["dl_date_qty"].ToString();
                    }

                    if (content!="")
                    {
                        text.content = "推送类型：签收附件提醒" + content;

                        rq.at = at;
                        rq.text = text;
                        rq.msgtype = "text";

                        Dictionary<string, string> urldic = new Dictionary<string, string>(); //URL获取dic
                        urldic.Add("function_description", "运单签收附件上传提醒");//查看URL配置表地址描述
                        DataTable url_dt = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                        if (url_dt.Rows.Count > 0)
                        {
                            string jsonText = JsonConvert.SerializeObject(rq);
                            var reAgv = DB.httpPost(url_dt.Rows[0]["interface_url"].ToString(), jsonText, "", "否");
                            TMStoDDmsgSend_RsDto psp = JsonConvert.DeserializeObject<TMStoDDmsgSend_RsDto>(reAgv);
                            if (psp.errcode > 0)
                            {
                                strMsg = "钉钉 ERR:" + psp.errmsg;
                            }
                        }
                    }

                    
                }
                
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
                    
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
